// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

import MainGame from "../game/MainGame";
import config from "../config/config";
import Guide from "../guide/Guide";

const { ccclass, property } = cc._decorator;

@ccclass
export default class CutUpVegetables extends cc.Component {

    @property(cc.Sprite)
    m_sample: cc.Sprite = null;
    @property(cc.Sprite)
    m_finish: cc.Sprite = null;
    @property(cc.Sprite)
    m_electric: cc.Sprite = null;
    @property(cc.SpriteFrame)
    m_frame: cc.SpriteFrame = null;

    n_nodeAni: cc.Animation = null;
    n_startCut: boolean = false;
    n_accelerate: boolean = false;
    n_cutSpeed: number = 0;
    n_accelerateSpeed: number = 0;
    n_mainGame: MainGame = null;

    start() {
        this.n_nodeAni = this.node.getComponent(cc.Animation);
        this.n_mainGame = cc.find("Canvas/game").getComponent(MainGame);
        this.node.on(cc.Node.EventType.TOUCH_START, this.touchStart, this);
        this.node.on(cc.Node.EventType.TOUCH_CANCEL, this.touchCancel, this);
        this.node.on(cc.Node.EventType.TOUCH_END, this.touchEnd, this);
        
    }

    init() {
        if (this.n_startCut) return;
        this.n_nodeAni.play();
        this.m_electric.node.active = true;
        this.n_startCut = true;
        this.m_finish.fillRange = 0;
        this.m_sample.node.active = true;
        this.n_cutSpeed = config.basicsTime / (config.orderCutUpTimeArr[0] / (1 + config.cook_1.additionTime / 100));
        this.n_accelerateSpeed = this.n_cutSpeed * 3;
    }
    touchStart(e: any) {
        if (e.getLocation().length > 1 || !this.n_startCut) return;
        this.n_accelerate = true;
    }
    touchCancel(e: any) {
        if (!this.n_startCut) return;
        this.n_accelerate = false;
    }
    touchEnd(e: any) {
        if (e.getLocation().length > 1 || !this.n_startCut) return;
        this.n_accelerate = false;
    }

    update() {
        if (this.n_startCut) {
            if (this.n_accelerate) {
                this.m_finish.fillRange += this.n_accelerateSpeed;
            } else {
                this.m_finish.fillRange += this.n_cutSpeed;
            }
            if (this.n_accelerate && config.gameType == "wx") {
                window["wx"].vibrateShort({
                    success() {
                        console.log("手机震动");
                    }
                })
            }
            if (this.m_finish.fillRange >= 1) {
                if (config.guide) {
                    Guide.instance.hideGuide();
                }
                if (config.orderCutUpTimeArr.length > 0) {
                    config.orderCutUpTimeArr.splice(0, 1);
                }
                this.n_nodeAni.stop();
                this.node.getComponent(cc.Sprite).spriteFrame = this.m_frame;
                this.m_electric.node.active = false;
                this.m_finish.fillRange = 1;
                this.n_startCut = false;
                this.n_accelerate = false;
                let haveCell = this.n_mainGame.cutUpVagetablesOver();
                if (haveCell) {
                    this.m_sample.node.active = false;
                    this.n_mainGame.countineCutUpVagetables();
                    this.n_mainGame.checkCookState(1);
                }
            }
        }
    }
}
